//
//  TenderFirstHelper.swift
//  SlaughterClient
//
//  Created by 邱仙凯 on 2020/3/28.
//  Copyright © 2020 zwb. All rights reserved.
//

import UIKit

 class ZyaTenderHelper: NSObject,zyatenderFilterable {
    
    
     let titles_jp = ["排序","区域","时间","更多"]
    let titles_zb = ["状态","区域","时间","更多"]
    
     
    var dropMenu:GHDropMenu?
    
    let source_tb_status = [
        "全部",
        "3日内",
        "7日内",
        "1月内",
        "3月内"
    ]
    let source_jp_status = [
        "全部",
        "3日内",
        "7日内",
        "1月内",
        "3月内"
    ]
    var callTableRefreshClosure:(()->())?
    
   
     func go2CarInfo(idkey:String,controller:UIViewController) {
         let vc = ZyaCarInfoVC()
         vc.id_key = idkey
         controller.navigationController?.pushViewController(vc, animated: true)
     }
    func go2DepositController(controller:UIViewController) {
        
        let vc = ZyaDepositVC()
        controller.navigationController?.pushViewController(vc, animated: true)
        
    }
     func go2FeedbackForm(zZbId:String = "",zZbNo:String = "",zTitle:String = "",controller:UIViewController) {
          let view = ZyaFeedbackFormVC()
         var one = FeedBaceModel(JSON:["":""])!
         one.z_zb_id = zZbId
         one.z_zb_no = zZbNo
         one.z_zb_title = zTitle
         view.one = one
         controller.navigationController?.pushViewController(view, animated: true)
     }
    
    func go2TenderTbController(listType:TBandJPType,controller:UIViewController,z_tb_id:String = "",z_zb_id:String = "",z_bid_id:String = "",excuteType:OperateType,model:ZyaZtbContentModel? = nil,refreshClosure:@escaping (()->())) {
       
        let realModel = model ?? ZyaZtbContentModel(JSON: ["":""])!
 
            if listType == .TB {
                var vc:ZyaTenderBaseForm!
                if realModel.zDtradeType == "3" {
                    vc =  YuanliaoTBVC()
                } else if realModel.zDtradeType == "4" {
                    vc =  WuliuTBVC()
                } else if realModel.zDtradeType == "5" {
                    vc =  ZyaYaoPinTBVC()
                }  else {
                    KSToast("类型缺失")
                    // 把物流当通用
                    vc =  WuliuTBVC()
                }
                vc.refreshClosure = refreshClosure
                vc.excuteType = excuteType
                
                if !z_tb_id.isEmpty {
                    vc.z_tb_id = z_tb_id
                }
                if !z_zb_id.isEmpty {
                    vc.z_zb_id = z_zb_id
                }
          
                if var mm = model {
                    if excuteType == .add {
                        mm.zContent = ""
                        mm.zName = ""
                        mm.zTel = ""
                        mm.zProvince = ""
                        mm.zCity = ""
                        mm.zAddress = ""
                    }
                    vc.one = mm
                }
                 controller.navigationController?.pushViewController(vc, animated: true)
                
  
                
                
            } else {
                // 竞拍
                let vc =  ZyaTenderJPVC()
                vc.refreshClosure = refreshClosure
                vc.excuteType = excuteType
                
           
                if !z_zb_id.isEmpty {
                    vc.z_zb_id = z_zb_id
                }
                if !z_bid_id.isEmpty {
                    vc.z_bid_id = z_bid_id
                }
                if var mm = model {
                    if excuteType == .add {
                        mm.zContent = ""
                        mm.zName = ""
                        mm.zTel = ""
                        mm.zProvince = ""
                        mm.zCity = ""
                        mm.zAddress = ""
                    }
                    vc.one = mm
                }
                 controller.navigationController?.pushViewController(vc, animated: true)
            }
            
         
            
        
       
        
    }
    
    
    
    func downloadFiles(zbFiles:[ZyaZbFile]) {
        if  zbFiles.count > 0 {
            
            let str1 = "\(zbFiles.first!.zPicUrl.unwrap)"
            KSNetwork.shared.download(url: str1, name: zbFiles.first!.zName.unwrap)
        } else {
            KSToast("没有文件可以下载")
        }
    }
    
    func setRecordMark(z_record_id:String,z_mark:String,z_type:String) {
        KSNetwork.shared.doRequest(API.setRecordMark(z_record_id: z_record_id, z_mark: z_mark, z_type: z_type),showHud: false) { (data:BaseResult<OperateResult>) in
            
        }
    }
    
  
    
    
    
    
    func unReferTb(controller:ZyaTenderListVC,idkey:String) {
        
        alertUtil(controller, title: "确认反提交？", operateS: [nameAction(name: "确定", action: { (_) in
            KSNetwork.shared.doRequest(API.referMytenderZbInfo(id_key: idkey, z_tag: "0")) { (data:BaseResult<OperateResult>) in
                if data.flag == TRUE {
                    
                    controller.tableView.mj_header?.beginRefreshing()
                    
                } else {
                    KSToast(data.message.unwrap)
                }
            }
        })])
        
    }
    func getHeaderHeight(webheight:Double,imgHeight:Double) -> CGFloat {
        return CGFloat(webheight + 800 + imgHeight)
    }
    
    
    
    func titleStrToAPI(listType:TBandJPType,titleStr:ZTBInnerType,startDate:String,endDate:String,maxValue:String,minValue:String,zprovince:String,zcity:String,searchTitle:String,page:Int,z_zb_id:String,z_status:String,outDate:String = "",category:String = "",followTag:String = "") -> API {
        
        var api:API?
        switch titleStr {
        case .zbListKHW,.recommendKHW:
            api = API.tender(.queryMytenderZbList(z_begin_dt: startDate, z_end_dt: endDate,z_province: zprovince, z_city: zcity, z_price_min: minValue, z_price_max: maxValue, page: "\(page)", size: "10", z_title: searchTitle,category:zyaleixingDic[category]!,z_status: z_status))
        case .jpListKHW:
            api = API.tender(.queryMytenderJpList(z_begin_dt: startDate, z_end_dt: endDate,z_province: zprovince, z_city: zcity, z_price_min: minValue, z_price_max: maxValue, page: "\(page)", size: "10", z_title: searchTitle, z_out_dt: outDate,z_status: z_status))
        case .jpMyjpKHW:
            api = API.tender(.queryMytenderJpJoinList(z_begin_dt: startDate, z_end_dt: endDate, z_city: zcity, z_price_min: minValue, z_price_max: maxValue, page: "\(page)", size: "10", z_title: searchTitle,z_status: z_status))
        case .zbMyPublishKHW:
            api = API.tender(.queryMyPublishZbList(z_begin_dt: startDate, z_end_dt: endDate, z_city: zcity, z_price_min: minValue, z_price_max: maxValue, page: "\(page)", size: "10", z_title: searchTitle))
        case .tbMytbKHW:
            api = API.tender(.queryMytenderZbBidList(z_begin_dt: startDate, z_end_dt: endDate, z_province: "",z_city: zcity, z_price_min: minValue, z_price_max: maxValue, page: "\(page)", size: "10", z_title: searchTitle,category:zyaleixingDic[category]!))
        case .tbRecordKHW:
            api = API.queryMytenderTbList(page: "\(page)", size: "10", z_zb_id: z_zb_id, z_title: searchTitle)
        case .tbMyMarkKHW,.jpMyMarkKHW:
            api = API.myFavoriteList(z_staff_id: USERINFO.usrid.unwrap,page: "\(page)", size: "10",z_type:listType == .TB ? "zhaobiao" : "jingpai",category:zyaleixingDic[category]!)
            
        default:
            print("22")
        }
        
        return api!
        
        
    }
  
 
    func setBtnTitle(listType:ZTBInnerType,zAuditMark:String,btn2:UIButton) -> String {
        
        if listType == .tbMytbKHW {
            btn2.setTitle("查看出价", for: .normal)
        } else if listType == .zbMyPublishKHW {
            
            if zAuditMark == "0" || zAuditMark == "2" {
                
                btn2.setTitle("编辑", for: .normal)
            } else if zAuditMark == "1" {
                
                btn2.setTitle("出价记录", for: .normal)
                
            } else if zAuditMark == "9" {
                btn2.setTitle("反提交", for: .normal)
            }
        } else if listType == .zbListKHW || listType == .recommendKHW {
            btn2.setTitle("", for: .normal)
            btn2.setBackgroundImage(#imageLiteral(resourceName: "dianhua"), for: .normal)
        } else {
            btn2.setTitle("", for: .normal)
        }
        return btn2.titleLabel!.text.unwrap
        
    }
    func go2TenderRecord(controller:UIViewController,id:String) {
     
    }
    func configRightItem4Tender(listType:TBandJPType,action:(()->())?) -> UIBarButtonItem? {
        return nil
    }
    func getInviteListAction(listType:TBandJPType,fromVC:UIViewController,z_zb_id:String) {
        
    }
  
    
    func configParam(s2:Int) {
        
        if s2 == 0 {
            self.filterPa!.startDate = ""
            self.filterPa!.endDate = ""
            
        } else if s2 == 1 {
            self.filterPa!.startDate = Date.todayPres(3)
            self.filterPa!.endDate = Date.today
            
        } else if s2 == 2 {
            self.filterPa!.startDate = Date.todayPres(7)
            self.filterPa!.endDate = Date.today
            
        } else if s2 == 3 {
            self.filterPa!.startDate = Date.preMonthsWithNowday(1)
            self.filterPa!.endDate = Date.today
            
        } else if s2 == 4 {
            self.filterPa!.startDate = Date.preMonthsWithNowday(3)
            self.filterPa!.endDate = Date.today
        }
        
    }
    
    
    func getContentStr(listType:TBandJPType,info:ZyaZtbContentModel) -> String {
        if listType == .TB {
           return info.zbInfo.unwrap
        } else {
            return info.jpInfo.unwrap
        }
        
    }
    
    
}

extension ZyaTenderHelper {
    
  
    
    func delegateSearchAction() {
        if let clo = self.callTableRefreshClosure {
            clo()
        }
        
    }
    
}


 

// 招出价过滤协议
protocol zyatenderFilterable where Self: GHDropMenuDelegate {
    
    
 
    var filterPa:zyatdfilterParam? { get set }
    
    
   
}


 
  extension zyatenderFilterable {
    
    
    
    
    
    var filterPa: zyatdfilterParam? {
        set {
            objc_setAssociatedObject(self, &RuntimeKeys.tenderfilterPaKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_COPY_NONATOMIC)
        }
        get {
            var fp = (objc_getAssociatedObject(self, &RuntimeKeys.tenderfilterPaKey) as? zyatdfilterParam)
            if fp.isNone {
                fp = zyatdfilterParam()
                fp!.zStatus = ""
            }
            return fp
            
        }
    }
    
    
    
  
    
}
func zyago2TenderInfoVC(listType:TBandJPType,fromVC:UIViewController,idkey:String,innerType:ZTBInnerType) {
    
    let vc = ZyaTenderInfoVC(listType: listType)
    vc.idkey = idkey
    vc.innerType = innerType
    fromVC.navigationController?.pushViewController(vc, animated: true)
}
